# Current, as of Thursday, May 28, 2020.
# rm(list = ls())
# setwd("~/Dropbox/Base Dropbox/List Experiment Paper (PS)/Conditional Accept/Replication Files (Carmines-Schmidt PS)/Power Analyses")
library(foreign)
library(car)
library(readstata13)
library(zeligverse)
library(stargazer)
library(apsrtable)
library(DeclareDesign)
library(DesignLibrary)
library(tidyr)
library(dplyr)
library(ggplot2)
library(estimatr)
library(randomizr)
library(fabricatr)
#----------
# Read in both the 2008 and 2012 CCAP modules as separate dataframes. 
ccap.2008 = read.dta13("CarminesSchmidtPS-replication-2008.dta", generate.factors = TRUE)
ccap.2012 = read.dta13("CarminesSchmidtPS-replication-2012.dta", generate.factors = TRUE)
#----------
# CCAP 2008, cleaning data for power analyses... 

# Drop respondents that weren't assigned to one of the five list experiment conditions.
ccap.2008 = subset(ccap.2008, treatment!="") # n = 1338

# Party ID, three-category (counting NA's as independents)
ccap.2008$pid3 = car::recode(ccap.2008$pid_7, "0 = 'Democratic'; 1 = 'Democratic'; 2 = 'Democratic'; 
                             3 = 'Independent'; 4 = 'Republican'; 5 = 'Republican'; 6 = 'Republican'; 
                             else = 'Independent'")
table(ccap.2008$pid3, ccap.2008$pid_7)
table(ccap.2008$pid3)

# Ideology, three-category (counting 'not sure' and NAs as moderates)  
ccap.2008$ideo3 = car::recode(ccap.2008$ideology, "0 = 'Liberal'; 1 = 'Liberal'; 
                              2 = 'Moderate'; 3 = 'Conservative'; 4 = 'Conservative'; 
                              else = 'Moderate'")
table(ccap.2008$ideo3, ccap.2008$ideology)
table(ccap.2008$ideology)

# Assignment to ANY of the sensitive-item conditions. 
ccap.2008$sensitive.item = ifelse(ccap.2008$treatment!="Control", "Sensitive-Item", "Control")
table(ccap.2008$sensitive.item)
table(ccap.2008$sensitive.item, ccap.2008$treatment)

# BASELINE or SENSITIVE-ITEM CONDITIONS (pooled) 
pooled.experiment.2008 = subset(ccap.2008, sensitive.item=="Control" | sensitive.item=="Sensitive-Item")
# BASELINE or WOMAN conditions.
women.experiment.2008 = subset(ccap.2008, treatment=="Women" | treatment=="Control")
# BASELINE or MUSLIM conditions. 
muslims.experiment.2008 = subset(ccap.2008, treatment=="Muslims" | treatment=="Control")
# BASELINE or 'GAY OR HOMOSEXUAL' conditions. 
gays.experiment.2008 = subset(ccap.2008, treatment=="Gays" | treatment=="Control")
# BASELINE or AFRICAN-AMERICAN conditions (just non-African-American respondents). 
afroam.experiment.2008 = subset(ccap.2008, (treatment=="African Americans" | treatment=="Control") & black!=1)

# REPUBLICANS in each experiment. 
pooled.experiment.gop.2008 = subset(pooled.experiment.2008, pid3=="Republican")
afroam.experiment.gop.2008 = subset(afroam.experiment.2008, pid3=="Republican")
gays.experiment.gop.2008 = subset(gays.experiment.2008, pid3=="Republican")
women.experiment.gop.2008 = subset(women.experiment.2008, pid3=="Republican")
muslims.experiment.gop.2008 = subset(muslims.experiment.2008, pid3=="Republican")
# DEMOCRATS in each experiment. 
pooled.experiment.dems.2008 = subset(pooled.experiment.2008, pid3=="Democratic")
afroam.experiment.dems.2008 = subset(afroam.experiment.2008, pid3=="Democratic")
gays.experiment.dems.2008 = subset(gays.experiment.2008, pid3=="Democratic")
women.experiment.dems.2008 = subset(women.experiment.2008, pid3=="Democratic")
muslims.experiment.dems.2008 = subset(muslims.experiment.2008, pid3=="Democratic")
# INDEPENDENTS in each experiment. 
pooled.experiment.inds.2008 = subset(pooled.experiment.2008, pid3=="Independent")
afroam.experiment.inds.2008 = subset(afroam.experiment.2008, pid3=="Independent")
gays.experiment.inds.2008 = subset(gays.experiment.2008, pid3=="Independent")
women.experiment.inds.2008 = subset(women.experiment.2008, pid3=="Independent")
muslims.experiment.inds.2008 = subset(muslims.experiment.2008, pid3=="Independent")
# CONSERVATIVES in each experiment. 
pooled.experiment.cons.2008 = subset(pooled.experiment.2008, ideo3=="Conservative")
afroam.experiment.cons.2008 = subset(afroam.experiment.2008, ideo3=="Conservative")
gays.experiment.cons.2008 = subset(gays.experiment.2008, ideo3=="Conservative")
women.experiment.cons.2008 = subset(women.experiment.2008, ideo3=="Conservative")
muslims.experiment.cons.2008 = subset(muslims.experiment.2008, ideo3=="Conservative")
# LIBERALS in each experiment. 
pooled.experiment.libs.2008 = subset(pooled.experiment.2008, ideo3=="Liberal")
afroam.experiment.libs.2008 = subset(afroam.experiment.2008, ideo3=="Liberal")
gays.experiment.libs.2008 = subset(gays.experiment.2008, ideo3=="Liberal")
women.experiment.libs.2008 = subset(women.experiment.2008, ideo3=="Liberal")
muslims.experiment.libs.2008 = subset(muslims.experiment.2008, ideo3=="Liberal")
# MODERATES in each experiment. 
pooled.experiment.mods.2008 = subset(pooled.experiment.2008, ideo3=="Moderate")
afroam.experiment.mods.2008 = subset(afroam.experiment.2008, ideo3=="Moderate")
gays.experiment.mods.2008 = subset(gays.experiment.2008, ideo3=="Moderate")
women.experiment.mods.2008 = subset(women.experiment.2008, ideo3=="Moderate")
muslims.experiment.mods.2008 = subset(muslims.experiment.2008, ideo3=="Moderate")
#----------
# CCAP 2012, cleaning data for power analyses... 

# Party ID, three-category (counting NA's as independents)
ccap.2012$pid3 = car::recode(ccap.2012$pid_7, "0 = 'Democratic'; 1 = 'Democratic'; 2 = 'Democratic'; 
                             3 = 'Independent'; 4 = 'Republican'; 5 = 'Republican'; 6 = 'Republican'; 
                             else = 'Independent'")
table(ccap.2012$pid3, ccap.2012$pid_7)
table(ccap.2012$pid3)

# Ideology, three-category (counting 'not sure' and NAs as moderates)  
ccap.2012$ideo3 = car::recode(ccap.2012$ideology, "0 = 'Liberal'; 1 = 'Liberal'; 
                              2 = 'Moderate'; 3 = 'Conservative'; 4 = 'Conservative'; 
                              else = 'Moderate'")
table(ccap.2012$ideo3, ccap.2012$ideology)
table(ccap.2012$ideology)

# Assignment to ANY of the sensitive-item conditions. 
ccap.2012$sensitive.item = ifelse(ccap.2012$treatment!="Control", "Sensitive-Item", "Control")
table(ccap.2012$sensitive.item)
table(ccap.2012$sensitive.item, ccap.2012$treatment)

# BASELINE or SENSITIVE-ITEM CONDITIONS (pooled) 
pooled.experiment.2012 = subset(ccap.2012, sensitive.item=="Control" | sensitive.item=="Sensitive-Item")
# BASELINE or MORMON conditions.
mormons.experiment.2012 = subset(ccap.2012, treatment=="Mormons" | treatment=="Control")
# BASELINE or MUSLIM conditions. 
muslims.experiment.2012 = subset(ccap.2012, treatment=="Muslims" | treatment=="Control")
# BASELINE or 'GAY OR HOMOSEXUAL' conditions. 
gays.experiment.2012 = subset(ccap.2012, treatment=="Gays" | treatment=="Control")
# BASELINE or AFRICAN-AMERICAN conditions (just non-African-American respondents). 
afroam.experiment.2012 = subset(ccap.2012, (treatment=="African Americans" | treatment=="Control") & black!=1)

# REPUBLICANS in each experiment. 
pooled.experiment.gop.2012 = subset(pooled.experiment.2012, pid3=="Republican")
afroam.experiment.gop.2012 = subset(afroam.experiment.2012, pid3=="Republican")
gays.experiment.gop.2012 = subset(gays.experiment.2012, pid3=="Republican")
mormons.experiment.gop.2012 = subset(mormons.experiment.2012, pid3=="Republican")
muslims.experiment.gop.2012 = subset(muslims.experiment.2012, pid3=="Republican")
# DEMOCRATS in each experiment. 
pooled.experiment.dems.2012 = subset(pooled.experiment.2012, pid3=="Democratic")
afroam.experiment.dems.2012 = subset(afroam.experiment.2012, pid3=="Democratic")
gays.experiment.dems.2012 = subset(gays.experiment.2012, pid3=="Democratic")
mormons.experiment.dems.2012 = subset(mormons.experiment.2012, pid3=="Democratic")
muslims.experiment.dems.2012 = subset(muslims.experiment.2012, pid3=="Democratic")
# INDEPENDENTS in each experiment. 
pooled.experiment.inds.2012 = subset(pooled.experiment.2012, pid3=="Independent")
afroam.experiment.inds.2012 = subset(afroam.experiment.2012, pid3=="Independent")
gays.experiment.inds.2012 = subset(gays.experiment.2012, pid3=="Independent")
mormons.experiment.inds.2012 = subset(mormons.experiment.2012, pid3=="Independent")
muslims.experiment.inds.2012 = subset(muslims.experiment.2012, pid3=="Independent")
# CONSERVATIVES in each experiment. 
pooled.experiment.cons.2012 = subset(pooled.experiment.2012, ideo3=="Conservative")
afroam.experiment.cons.2012 = subset(afroam.experiment.2012, ideo3=="Conservative")
gays.experiment.cons.2012 = subset(gays.experiment.2012, ideo3=="Conservative")
mormons.experiment.cons.2012 = subset(mormons.experiment.2012, ideo3=="Conservative")
muslims.experiment.cons.2012 = subset(muslims.experiment.2012, ideo3=="Conservative")
# LIBERALS in each experiment. 
pooled.experiment.libs.2012 = subset(pooled.experiment.2012, ideo3=="Liberal")
afroam.experiment.libs.2012 = subset(afroam.experiment.2012, ideo3=="Liberal")
gays.experiment.libs.2012 = subset(gays.experiment.2012, ideo3=="Liberal")
mormons.experiment.libs.2012 = subset(mormons.experiment.2012, ideo3=="Liberal")
muslims.experiment.libs.2012 = subset(muslims.experiment.2012, ideo3=="Liberal")
# MODERATES in each experiment. 
pooled.experiment.mods.2012 = subset(pooled.experiment.2012, ideo3=="Moderate")
afroam.experiment.mods.2012 = subset(afroam.experiment.2012, ideo3=="Moderate")
gays.experiment.mods.2012 = subset(gays.experiment.2012, ideo3=="Moderate")
mormons.experiment.mods.2012 = subset(mormons.experiment.2012, ideo3=="Moderate")
muslims.experiment.mods.2012 = subset(muslims.experiment.2012, ideo3=="Moderate")
#----------
# Function to simulate different designs, varying both N and the true effect size (signal-to-noise ratio) 
# in the data-generating process. 
designer = function(our.data, effect_size) {
  pop = declare_population( 
    N = length(our.data$sensitive.item),
    noise = rnorm(N)
  )
  pos =  declare_potential_outcomes(Y ~ effect_size*Z + noise) 
  # assgn = declare_assignment(prob = 0.5)
  assgn = declare_assignment(m = length(our.data$sensitive.item[our.data$sensitive.item=="Sensitive-Item"]))
  mand = declare_estimand(ate = mean(Y_Z_1 - Y_Z_0))
  mator = declare_estimator(
    Y ~ Z, 
    model = difference_in_means,
    estimand = c("ate")
  )
  pop + pos + assgn + mand + mator 
}
# Diagnosis of design; to diagnose correctly, we must input (in the "designer" function) both the relevant dataframe, 
# and the effect size we're looking for. Example here: 
ptm = proc.time()
set.seed(47408)
our.diagnosis = diagnose_design(designer(pooled.experiment.2012, 0.2),
                                diagnosands = declare_diagnosands(select = c(power)),
                                bootstrap_sims = FALSE)
our.diagnosis
proc.time() - ptm
# For the 2012 CCAP, were we investigating the difference-of-means between respondents in ALL sensitive-item conditions (pooled) 
# and the baseline condition, we would have power = 0.68 to detect an effect size of 0.2. 
#----------
# Expand the design to encompass *all* of our samples and subsamples, across the range of ATEs (in 0.05 increments).
set.seed(47408)
designs = expand_design(designer, 
                        our.data = list(pooled.experiment.2008, # BASELINE and POOLED SENSITIVE-ITEM conditions, 2008
                                        pooled.experiment.gop.2008,
                                        pooled.experiment.dems.2008,
                                        pooled.experiment.inds.2008,
                                        pooled.experiment.cons.2008,
                                        pooled.experiment.libs.2008,
                                        pooled.experiment.mods.2008,
                                        afroam.experiment.2008, # BASELINE and AFRICAN AMERICAN conditions, 2008.
                                        afroam.experiment.gop.2008,
                                        afroam.experiment.dems.2008,
                                        afroam.experiment.inds.2008,
                                        afroam.experiment.cons.2008,
                                        afroam.experiment.libs.2008,
                                        afroam.experiment.mods.2008,
                                        gays.experiment.2008, # BASELINE and "GAY OR HOMOSEXUAL" conditions, 2008.
                                        gays.experiment.gop.2008,
                                        gays.experiment.dems.2008,
                                        gays.experiment.inds.2008,
                                        gays.experiment.cons.2008,
                                        gays.experiment.libs.2008,
                                        gays.experiment.mods.2008,
                                        women.experiment.2008, # BASELINE and WOMAN conditions, 2008.
                                        women.experiment.gop.2008,
                                        women.experiment.dems.2008,
                                        women.experiment.inds.2008,
                                        women.experiment.cons.2008,
                                        women.experiment.libs.2008,
                                        women.experiment.mods.2008,
                                        muslims.experiment.2008, # BASELINE and MUSLIMS conditions, 2008.
                                        muslims.experiment.gop.2008,
                                        muslims.experiment.dems.2008,
                                        muslims.experiment.inds.2008,
                                        muslims.experiment.cons.2008,
                                        muslims.experiment.libs.2008,
                                        muslims.experiment.mods.2008,
                                        pooled.experiment.2012, # BASELINE and POOLED SENSITIVE-ITEM conditions, 2012
                                        pooled.experiment.gop.2012,
                                        pooled.experiment.dems.2012,
                                        pooled.experiment.inds.2012,
                                        pooled.experiment.cons.2012,
                                        pooled.experiment.libs.2012,
                                        pooled.experiment.mods.2012,
                                        afroam.experiment.2012, # BASELINE and AFRICAN AMERICAN conditions, 2012
                                        afroam.experiment.gop.2012,
                                        afroam.experiment.dems.2012,
                                        afroam.experiment.inds.2012,
                                        afroam.experiment.cons.2012,
                                        afroam.experiment.libs.2012,
                                        afroam.experiment.mods.2012,
                                        gays.experiment.2012, # BASELINE and 'GAY OR HOMOSEXUAL' conditions, 2012.
                                        gays.experiment.gop.2012,
                                        gays.experiment.dems.2012,
                                        gays.experiment.inds.2012,
                                        gays.experiment.cons.2012,
                                        gays.experiment.libs.2012,
                                        gays.experiment.mods.2012,
                                        mormons.experiment.2012, # BASELINE and MORMON conditions, 2012.
                                        mormons.experiment.gop.2012,
                                        mormons.experiment.dems.2012,
                                        mormons.experiment.inds.2012,
                                        mormons.experiment.cons.2012,
                                        mormons.experiment.libs.2012,
                                        mormons.experiment.mods.2012,
                                        muslims.experiment.2012, # BASELINE and MUSLIM conditions, 2012. 
                                        muslims.experiment.gop.2012,
                                        muslims.experiment.dems.2012,
                                        muslims.experiment.inds.2012,
                                        muslims.experiment.cons.2012,
                                        muslims.experiment.libs.2012,
                                        muslims.experiment.mods.2012),
                        effect_size = seq(0.20, 0.90, 0.05))
head(names(designs))
tail(names(designs))
# This gives us 1050 designs total. That's consistent with 15 possible values of the ATE, 
# across 70 different subgroups (seven for each sample: FULL SAMPLE; REPUBLICANS, DEMOCRATS, 
# INDEPENDENTS, CONSERVATIVES, LIBERALS, MODERATES). 
#----------
# Diagnoses. Start the clock!
ptm = proc.time()
set.seed(47408)
our.diagnoses = diagnose_design(designs, diagnosands = declare_diagnosands(select = c(power)),
                                bootstrap_sims = FALSE)
proc.time() - ptm # Barring any issues, this should take less than two hours to run.
# Create a dataset based on the diagnoses for each design. 
plot.data = our.diagnoses$diagnosands_df
head(plot.data)
write.csv(plot.data, file = "2008-2012-Power-Analyses.csv")
names(plot.data)
#----------
# Clear everything from working memory and read in the new data...
rm(list = ls())
plot.data = read.csv("2008-2012-Power-Analyses.csv")
names(plot.data)
table(plot.data$our.data)
library(dplyr)
library(tidyr)
library(ggplot2)
#----------
# AFRICAN AMERICAN list experiment, 2008. 
# Subset the data to include only power estimates for the relevant experiment. 
plot.afroam.2008 = subset(plot.data, our.data=='afroam.experiment.2008' | 
                            our.data=='afroam.experiment.cons.2008' | 
                            our.data=='afroam.experiment.libs.2008' | 
                            our.data=='afroam.experiment.mods.2008' |
                            our.data=='afroam.experiment.gop.2008' |
                            our.data=='afroam.experiment.dems.2008' |
                            our.data=='afroam.experiment.inds.2008')
# Rename the indicator variable denoting the samples or subsamples pertaining
# to the power estimates. 
plot.afroam.2008$our.data = car::recode(plot.afroam.2008$our.data, 
                                        "'afroam.experiment.2008' = 'Full Sample';
                                        'afroam.experiment.cons.2008' = 'Conservatives';
                                        'afroam.experiment.libs.2008' = 'Liberals';
                                        'afroam.experiment.mods.2008' = 'Moderates (or Unsure)';
                                        'afroam.experiment.gop.2008' = 'Republicans';
                                        'afroam.experiment.dems.2008' = 'Democrats';
                                        'afroam.experiment.inds.2008' = 'Independents (or Unsure)'")
# Reorder the levels in this indicator variable. 
plot.afroam.2008$our.data = factor(plot.afroam.2008$our.data, levels = c("Full Sample",
                                                                         "Democrats",
                                                                         "Independents (or Unsure)",
                                                                         "Republicans",
                                                                         "Liberals",
                                                                         "Moderates (or Unsure)",
                                                                         "Conservatives"))
table(plot.afroam.2008$our.data)

# Plot power as a function of effect size, and facet the plot by sample or subsample we're analyzing.
our.plot = ggplot(data = plot.afroam.2008, aes(effect_size, power, group = our.data)) +
  # geom_line() +
  geom_hline(yintercept = 0.80, linetype = "dashed") + 
  ggtitle("'African American Candidate' List Experiment, CCAP 2008 (Non-Black Respondents Only): \nPower for Difference-of-Means Estimator, by ATE and Experiment Subset") +
  xlab("Average Treatment Effect") + 
  ylab("Power (Dashed Line is d = 0.80)") +
  facet_wrap(~ our.data, nrow = 2) + 
  theme_minimal() +
  theme(legend.title = element_blank()) + 
  geom_smooth(method = "loess", color = "black", size = 0.5) + 
  theme(strip.text = element_text(face = "bold", size = 10))
our.plot
                             
# Plot the intersection between the "80 percent power" line and the loess line.
smooths = ggplot_build(our.plot)
smooths = smooths$data[[2]]
# Approximate the intercept, representing the ATE that can be assessed at 80 percent power. 
smooth_1_i = c()
for (i in 1:7) {
  smooth_1_i[i] = smooths$x[which.min(abs(smooths$y[smooths$PANEL==i] - 0.8))]
}
round(smooth_1_i, 2)

# Annotations. 
dat_text = data.frame(
  label = c("d = 0.80 for \ndetecting ATE = 0.26",
            "d = 0.80 for \ndetecting ATE = 0.39",
            "d = 0.80 for \ndetecting ATE = 0.71",
            "d = 0.80 for \ndetecting ATE = 0.39",
            "d = 0.80 for \ndetecting ATE = 0.62",
            "d = 0.80 for \ndetecting ATE = 0.39",
            "d = 0.80 for \ndetecting ATE = 0.41"),
  our.data   = c("Full Sample", "Democrats", "Independents (or Unsure)", "Republicans", 
                 "Liberals", "Moderates (or Unsure)", "Conservatives")
)

# Add annotations to plot based on power analysis above. 
our.plot = our.plot + geom_text(
  data    = dat_text,
  mapping = aes(x = 1, y = 0, label = label),
  hjust   = "inward",
  vjust   = "inward",
  size = 3
)
our.plot = our.plot + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) 
our.plot
ggsave("2008-AfricanAmerican-Power-Analysis.pdf", plot = our.plot, height = 5, width = 8.5, units = c("in"))
#----------
# GAY OR HOMOSEXUAL list experiment, 2008. 
# Subset the data to include only power estimates for the relevant experiment. 
plot.gays.2008 = subset(plot.data, our.data=='gays.experiment.2008' | 
                          our.data=='gays.experiment.cons.2008' | 
                          our.data=='gays.experiment.libs.2008' | 
                          our.data=='gays.experiment.mods.2008' |
                          our.data=='gays.experiment.gop.2008' |
                          our.data=='gays.experiment.dems.2008' |
                          our.data=='gays.experiment.inds.2008')
# Rename the indicator variable denoting the samples or subsamples pertaining
# to the power estimates. 
plot.gays.2008$our.data = car::recode(plot.gays.2008$our.data, 
                                      "'gays.experiment.2008' = 'Full Sample';
                                        'gays.experiment.cons.2008' = 'Conservatives';
                                        'gays.experiment.libs.2008' = 'Liberals';
                                        'gays.experiment.mods.2008' = 'Moderates (or Unsure)';
                                        'gays.experiment.gop.2008' = 'Republicans';
                                        'gays.experiment.dems.2008' = 'Democrats';
                                        'gays.experiment.inds.2008' = 'Independents (or Unsure)'")
# Reorder the levels in this indicator variable. 
plot.gays.2008$our.data = factor(plot.gays.2008$our.data, levels = c("Full Sample",
                                                                     "Democrats",
                                                                     "Independents (or Unsure)",
                                                                     "Republicans",
                                                                     "Liberals",
                                                                     "Moderates (or Unsure)",
                                                                     "Conservatives"))
table(plot.gays.2008$our.data)

# Plot power as a function of effect size, and facet the plot by sample or subsample we're analyzing.
our.plot = ggplot(data = plot.gays.2008, aes(effect_size, power, group = our.data)) +
  # geom_line() +
  geom_hline(yintercept = 0.80, linetype = "dashed") + 
  ggtitle("'Gay-or-Homosexual Candidate' List Experiment, CCAP 2008: \nPower for Difference-of-Means Estimator, by ATE and Experiment Subset") +
  xlab("Average Treatment Effect") + 
  ylab("Power (Dashed Line is d = 0.80)") +
  facet_wrap(~ our.data, nrow = 2) + 
  theme_minimal() +
  theme(legend.title = element_blank()) + 
  geom_smooth(method = "loess", color = "black", size = 0.5) + 
  theme(strip.text = element_text(face = "bold", size = 10))
our.plot

# Plot the intersection between the "80 percent power" line and the loess line.
smooths = ggplot_build(our.plot)
smooths = smooths$data[[2]]
# Approximate the intercept, representing the ATE that can be assessed at 80 percent power. 
smooth_1_i = c()
for (i in 1:7) {
  smooth_1_i[i] = smooths$x[which.min(abs(smooths$y[smooths$PANEL==i] - 0.8))]
}
round(smooth_1_i, 2)

# Annotations. 
dat_text = data.frame(
  label = c("d = 0.80 for \ndetecting ATE = 0.26",
            "d = 0.80 for \ndetecting ATE = 0.38",
            "d = 0.80 for \ndetecting ATE = 0.67",
            "d = 0.80 for \ndetecting ATE = 0.39",
            "d = 0.80 for \ndetecting ATE = 0.55",
            "d = 0.80 for \ndetecting ATE = 0.38",
            "d = 0.80 for \ndetecting ATE = 0.42"),
  our.data   = c("Full Sample", "Democrats", "Independents (or Unsure)", "Republicans", 
                 "Liberals", "Moderates (or Unsure)", "Conservatives")
)

# Add annotations to plot based on power analysis above. 
our.plot = our.plot + geom_text(
  data    = dat_text,
  mapping = aes(x = 1, y = 0, label = label),
  hjust   = "inward",
  vjust   = "inward",
  size = 3
)
our.plot = our.plot + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) 
our.plot
ggsave("2008-Gays-Power-Analysis.pdf", plot = our.plot, height = 5, width = 8.5, units = c("in"))
#----------
# MUSLIM list experiment, 2008. 
# Subset the data to include only power estimates for the relevant experiment. 
plot.muslims.2008 = subset(plot.data, our.data=='muslims.experiment.2008' | 
                             our.data=='muslims.experiment.cons.2008' | 
                             our.data=='muslims.experiment.libs.2008' | 
                             our.data=='muslims.experiment.mods.2008' |
                             our.data=='muslims.experiment.gop.2008' |
                             our.data=='muslims.experiment.dems.2008' |
                             our.data=='muslims.experiment.inds.2008')
# Rename the indicator variable denoting the samples or subsamples pertaining
# to the power estimates. 
plot.muslims.2008$our.data = car::recode(plot.muslims.2008$our.data, 
                                         "'muslims.experiment.2008' = 'Full Sample';
                                        'muslims.experiment.cons.2008' = 'Conservatives';
                                        'muslims.experiment.libs.2008' = 'Liberals';
                                        'muslims.experiment.mods.2008' = 'Moderates (or Unsure)';
                                        'muslims.experiment.gop.2008' = 'Republicans';
                                        'muslims.experiment.dems.2008' = 'Democrats';
                                        'muslims.experiment.inds.2008' = 'Independents (or Unsure)'")
# Reorder the levels in this indicator variable. 
plot.muslims.2008$our.data = factor(plot.muslims.2008$our.data, levels = c("Full Sample",
                                                                           "Democrats",
                                                                           "Independents (or Unsure)",
                                                                           "Republicans",
                                                                           "Liberals",
                                                                           "Moderates (or Unsure)",
                                                                           "Conservatives"))
table(plot.muslims.2008$our.data)

# Plot power as a function of effect size, and facet the plot by sample or subsample we're analyzing.
our.plot = ggplot(data = plot.muslims.2008, aes(effect_size, power, group = our.data)) +
  # geom_line() +
  geom_hline(yintercept = 0.80, linetype = "dashed") + 
  ggtitle("'Muslim Candidate' List Experiment, CCAP 2008: \nPower for Difference-of-Means Estimator, by ATE and Experiment Subset") +
  xlab("Average Treatment Effect") + 
  ylab("Power (Dashed Line is d = 0.80)") +
  facet_wrap(~ our.data, nrow = 2) + 
  theme_minimal() +
  theme(legend.title = element_blank()) + 
  geom_smooth(method = "loess", color = "black", size = 0.5) + 
  theme(strip.text = element_text(face = "bold", size = 10))
our.plot

# Plot the intersection between the "80 percent power" line and the loess line.
smooths = ggplot_build(our.plot)
smooths = smooths$data[[2]]
# Approximate the intercept, representing the ATE that can be assessed at 80 percent power. 
smooth_1_i = c()
for (i in 1:7) {
  smooth_1_i[i] = smooths$x[which.min(abs(smooths$y[smooths$PANEL==i] - 0.8))]
}
round(smooth_1_i, 2)

# Annotations. 
dat_text = data.frame(
  label = c("d = 0.80 for \ndetecting ATE = 0.25",
            "d = 0.80 for \ndetecting ATE = 0.37",
            "d = 0.80 for \ndetecting ATE = 0.68",
            "d = 0.80 for \ndetecting ATE = 0.39",
            "d = 0.80 for \ndetecting ATE = 0.53",
            "d = 0.80 for \ndetecting ATE = 0.38",
            "d = 0.80 for \ndetecting ATE = 0.41"),
  our.data   = c("Full Sample", "Democrats", "Independents (or Unsure)", "Republicans", 
                 "Liberals", "Moderates (or Unsure)", "Conservatives")
)

# Add annotations to plot based on power analysis above. 
our.plot = our.plot + geom_text(
  data    = dat_text,
  mapping = aes(x = 1, y = 0, label = label),
  hjust   = "inward",
  vjust   = "inward",
  size = 3
)
our.plot = our.plot + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) 
our.plot
ggsave("2008-Muslims-Power-Analysis.pdf", plot = our.plot, height = 5, width = 8.5, units = c("in"))
#----------
# FEMALE list experiment, 2008. 
# Subset the data to include only power estimates for the relevant experiment. 
plot.women.2008 = subset(plot.data, our.data=='women.experiment.2008' | 
                           our.data=='women.experiment.cons.2008' | 
                           our.data=='women.experiment.libs.2008' | 
                           our.data=='women.experiment.mods.2008' |
                           our.data=='women.experiment.gop.2008' |
                           our.data=='women.experiment.dems.2008' |
                           our.data=='women.experiment.inds.2008')
# Rename the indicator variable denoting the samples or subsamples pertaining
# to the power estimates. 
plot.women.2008$our.data = car::recode(plot.women.2008$our.data, 
                                       "'women.experiment.2008' = 'Full Sample';
                                        'women.experiment.cons.2008' = 'Conservatives';
                                        'women.experiment.libs.2008' = 'Liberals';
                                        'women.experiment.mods.2008' = 'Moderates (or Unsure)';
                                        'women.experiment.gop.2008' = 'Republicans';
                                        'women.experiment.dems.2008' = 'Democrats';
                                        'women.experiment.inds.2008' = 'Independents (or Unsure)'")
# Reorder the levels in this indicator variable. 
plot.women.2008$our.data = factor(plot.women.2008$our.data, levels = c("Full Sample",
                                                                       "Democrats",
                                                                       "Independents (or Unsure)",
                                                                       "Republicans",
                                                                       "Liberals",
                                                                       "Moderates (or Unsure)",
                                                                       "Conservatives"))
table(plot.women.2008$our.data)

# Plot power as a function of effect size, and facet the plot by sample or subsample we're analyzing.
our.plot = ggplot(data = plot.women.2008, aes(effect_size, power, group = our.data)) +
  # geom_line() +
  geom_hline(yintercept = 0.80, linetype = "dashed") + 
  ggtitle("'Female Candidate' List Experiment, CCAP 2008: \nPower for Difference-of-Means Estimator, by ATE and Experiment Subset") +
  xlab("Average Treatment Effect") + 
  ylab("Power (Dashed Line is d = 0.80)") +
  facet_wrap(~ our.data, nrow = 2) + 
  theme_minimal() +
  theme(legend.title = element_blank()) + 
  geom_smooth(method = "loess", color = "black", size = 0.5) + 
  theme(strip.text = element_text(face = "bold", size = 10))
our.plot

# Plot the intersection between the "80 percent power" line and the loess line.
smooths = ggplot_build(our.plot)
smooths = smooths$data[[2]]
# Approximate the intercept, representing the ATE that can be assessed at 80 percent power. 
smooth_1_i = c()
for (i in 1:7) {
  smooth_1_i[i] = smooths$x[which.min(abs(smooths$y[smooths$PANEL==i] - 0.8))]
}
round(smooth_1_i, 2)

# Annotations. 
dat_text = data.frame(
  label = c("d = 0.80 for \ndetecting ATE = 0.24",
            "d = 0.80 for \ndetecting ATE = 0.36",
            "d = 0.80 for \ndetecting ATE = 0.64",
            "d = 0.80 for \ndetecting ATE = 0.38",
            "d = 0.80 for \ndetecting ATE = 0.52",
            "d = 0.80 for \ndetecting ATE = 0.37",
            "d = 0.80 for \ndetecting ATE = 0.40"),
  our.data   = c("Full Sample", "Democrats", "Independents (or Unsure)", "Republicans", 
                 "Liberals", "Moderates (or Unsure)", "Conservatives")
)

# Add annotations to plot based on power analysis above. 
our.plot = our.plot + geom_text(
  data    = dat_text,
  mapping = aes(x = 1, y = 0, label = label),
  hjust   = "inward",
  vjust   = "inward",
  size = 3
)
our.plot = our.plot + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) 
our.plot
ggsave("2008-Female-Power-Analysis.pdf", plot = our.plot, height = 5, width = 8.5, units = c("in"))
#----------
# POOLED SENSITIVE-ITEMS "prejudice index," 2008. 
# Subset the data to include only power estimates for the relevant experiment. 
plot.pooled.2008 = subset(plot.data, our.data=='pooled.experiment.2008' | 
                            our.data=='pooled.experiment.cons.2008' | 
                            our.data=='pooled.experiment.libs.2008' | 
                            our.data=='pooled.experiment.mods.2008' |
                            our.data=='pooled.experiment.gop.2008' |
                            our.data=='pooled.experiment.dems.2008' |
                            our.data=='pooled.experiment.inds.2008')
# Rename the indicator variable denoting the samples or subsamples pertaining
# to the power estimates. 
plot.pooled.2008$our.data = car::recode(plot.pooled.2008$our.data, 
                                        "'pooled.experiment.2008' = 'Full Sample';
                                        'pooled.experiment.cons.2008' = 'Conservatives';
                                        'pooled.experiment.libs.2008' = 'Liberals';
                                        'pooled.experiment.mods.2008' = 'Moderates (or Unsure)';
                                        'pooled.experiment.gop.2008' = 'Republicans';
                                        'pooled.experiment.dems.2008' = 'Democrats';
                                        'pooled.experiment.inds.2008' = 'Independents (or Unsure)'")
# Reorder the levels in this indicator variable. 
plot.pooled.2008$our.data = factor(plot.pooled.2008$our.data, levels = c("Full Sample",
                                                                         "Democrats",
                                                                         "Independents (or Unsure)",
                                                                         "Republicans",
                                                                         "Liberals",
                                                                         "Moderates (or Unsure)",
                                                                         "Conservatives"))
table(plot.pooled.2008$our.data)

# Plot power as a function of effect size, and facet the plot by sample or subsample we're analyzing.
our.plot = ggplot(data = plot.pooled.2008, aes(effect_size, power, group = our.data)) +
  # geom_line() +
  geom_hline(yintercept = 0.80, linetype = "dashed") + 
  ggtitle("Generalized Prejudice: Pooled Sensitive-Items from List Experiments, CCAP 2008: \nPower for Difference-of-Means Estimator, by ATE and Experiment Subset") +
  xlab("Average Treatment Effect") + 
  ylab("Power (Dashed Line is d = 0.80)") +
  facet_wrap(~ our.data, nrow = 2) + 
  theme_minimal() +
  theme(legend.title = element_blank()) + 
  geom_smooth(method = "loess", color = "black", size = 0.5) + 
  theme(strip.text = element_text(face = "bold", size = 10))
our.plot

# Plot the intersection between the "80 percent power" line and the loess line.
smooths = ggplot_build(our.plot)
smooths = smooths$data[[2]]
# Approximate the intercept, representing the ATE that can be assessed at 80 percent power. 
smooth_1_i = c()
for (i in 1:7) {
  smooth_1_i[i] = smooths$x[which.min(abs(smooths$y[smooths$PANEL==i] - 0.8))]
}
round(smooth_1_i, 2)

# Annotations. 
dat_text = data.frame(
  label = c("d = 0.80 for \ndetecting ATE = 0.20",
            "d = 0.80 for \ndetecting ATE = 0.30",
            "d = 0.80 for \ndetecting ATE = 0.53",
            "d = 0.80 for \ndetecting ATE = 0.32",
            "d = 0.80 for \ndetecting ATE = 0.44",
            "d = 0.80 for \ndetecting ATE = 0.30",
            "d = 0.80 for \ndetecting ATE = 0.33"),
  our.data   = c("Full Sample", "Democrats", "Independents (or Unsure)", "Republicans", 
                 "Liberals", "Moderates (or Unsure)", "Conservatives")
)

# Add annotations to plot based on power analysis above. 
our.plot = our.plot + geom_text(
  data    = dat_text,
  mapping = aes(x = 1, y = 0, label = label),
  hjust   = "inward",
  vjust   = "inward",
  size = 3
)
our.plot = our.plot + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) 
our.plot
ggsave("2008-Pooled-Power-Analysis.pdf", plot = our.plot, height = 5, width = 8.5, units = c("in"))
#----------
# AFRICAN AMERICAN list experiment, 2012. 
# Subset the data to include only power estimates for the relevant experiment. 
plot.afroam.2012 = subset(plot.data, our.data=='afroam.experiment.2012' | 
                            our.data=='afroam.experiment.cons.2012' | 
                            our.data=='afroam.experiment.libs.2012' | 
                            our.data=='afroam.experiment.mods.2012' |
                            our.data=='afroam.experiment.gop.2012' |
                            our.data=='afroam.experiment.dems.2012' |
                            our.data=='afroam.experiment.inds.2012')
# Rename the indicator variable denoting the samples or subsamples pertaining
# to the power estimates. 
plot.afroam.2012$our.data = car::recode(plot.afroam.2012$our.data, 
                                        "'afroam.experiment.2012' = 'Full Sample';
                                        'afroam.experiment.cons.2012' = 'Conservatives';
                                        'afroam.experiment.libs.2012' = 'Liberals';
                                        'afroam.experiment.mods.2012' = 'Moderates (or Unsure)';
                                        'afroam.experiment.gop.2012' = 'Republicans';
                                        'afroam.experiment.dems.2012' = 'Democrats';
                                        'afroam.experiment.inds.2012' = 'Independents (or Unsure)'")
# Reorder the levels in this indicator variable. 
plot.afroam.2012$our.data = factor(plot.afroam.2012$our.data, levels = c("Full Sample",
                                                                         "Democrats",
                                                                         "Independents (or Unsure)",
                                                                         "Republicans",
                                                                         "Liberals",
                                                                         "Moderates (or Unsure)",
                                                                         "Conservatives"))
table(plot.afroam.2012$our.data)

# Plot power as a function of effect size, and facet the plot by sample or subsample we're analyzing.
our.plot = ggplot(data = plot.afroam.2012, aes(effect_size, power, group = our.data)) +
  # geom_line() +
  geom_hline(yintercept = 0.80, linetype = "dashed") + 
  ggtitle("'African American Candidate' List Experiment, CCAP 2012 (Non-Black Respondents Only): \nPower for Difference-of-Means Estimator, by ATE and Experiment Subset") +
  xlab("Average Treatment Effect") + 
  ylab("Power (Dashed Line is d = 0.80)") +
  facet_wrap(~ our.data, nrow = 2) + 
  theme_minimal() +
  theme(legend.title = element_blank()) + 
  geom_smooth(method = "loess", color = "black", size = 0.5) + 
  theme(strip.text = element_text(face = "bold", size = 10))
our.plot

# Plot the intersection between the "80 percent power" line and the loess line.
smooths = ggplot_build(our.plot)
smooths = smooths$data[[2]]
# Approximate the intercept, representing the ATE that can be assessed at 80 percent power. 
smooth_1_i = c()
for (i in 1:7) {
  smooth_1_i[i] = smooths$x[which.min(abs(smooths$y[smooths$PANEL==i] - 0.8))]
}
round(smooth_1_i, 2)

# Annotations. 
dat_text = data.frame(
  label = c("d = 0.80 for \ndetecting ATE = 0.32",
            "d = 0.80 for \ndetecting ATE = 0.48",
            "d = 0.80 for \ndetecting ATE = 0.82",
            "d = 0.80 for \ndetecting ATE = 0.44",
            "d = 0.80 for \ndetecting ATE = 0.63",
            "d = 0.80 for \ndetecting ATE = 0.55",
            "d = 0.80 for \ndetecting ATE = 0.47"),
  our.data   = c("Full Sample", "Democrats", "Independents (or Unsure)", "Republicans", 
                 "Liberals", "Moderates (or Unsure)", "Conservatives")
)

# Add annotations to plot based on power analysis above. 
our.plot = our.plot + geom_text(
  data    = dat_text,
  mapping = aes(x = 1, y = 0, label = label),
  hjust   = "inward",
  vjust   = "inward",
  size = 3
)
our.plot = our.plot + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) 
our.plot
ggsave("2012-AfricanAmerican-Power-Analysis.pdf", plot = our.plot, height = 5, width = 8.5, units = c("in"))
#----------
# GAY OR HOMOSEXUAL list experiment, 2012. 
# Subset the data to include only power estimates for the relevant experiment. 
plot.gays.2012 = subset(plot.data, our.data=='gays.experiment.2012' | 
                          our.data=='gays.experiment.cons.2012' | 
                          our.data=='gays.experiment.libs.2012' | 
                          our.data=='gays.experiment.mods.2012' |
                          our.data=='gays.experiment.gop.2012' |
                          our.data=='gays.experiment.dems.2012' |
                          our.data=='gays.experiment.inds.2012')
# Rename the indicator variable denoting the samples or subsamples pertaining
# to the power estimates. 
plot.gays.2012$our.data = car::recode(plot.gays.2012$our.data, 
                                      "'gays.experiment.2012' = 'Full Sample';
                                        'gays.experiment.cons.2012' = 'Conservatives';
                                        'gays.experiment.libs.2012' = 'Liberals';
                                        'gays.experiment.mods.2012' = 'Moderates (or Unsure)';
                                        'gays.experiment.gop.2012' = 'Republicans';
                                        'gays.experiment.dems.2012' = 'Democrats';
                                        'gays.experiment.inds.2012' = 'Independents (or Unsure)'")
# Reorder the levels in this indicator variable. 
plot.gays.2012$our.data = factor(plot.gays.2012$our.data, levels = c("Full Sample",
                                                                     "Democrats",
                                                                     "Independents (or Unsure)",
                                                                     "Republicans",
                                                                     "Liberals",
                                                                     "Moderates (or Unsure)",
                                                                     "Conservatives"))
table(plot.gays.2012$our.data)

# Plot power as a function of effect size, and facet the plot by sample or subsample we're analyzing.
our.plot = ggplot(data = plot.gays.2012, aes(effect_size, power, group = our.data)) +
  # geom_line() +
  geom_hline(yintercept = 0.80, linetype = "dashed") + 
  ggtitle("'Gay-or-Homosexual Candidate' List Experiment, CCAP 2012: \nPower for Difference-of-Means Estimator, by ATE and Experiment Subset") +
  xlab("Average Treatment Effect") + 
  ylab("Power (Dashed Line is d = 0.80)") +
  facet_wrap(~ our.data, nrow = 2) + 
  theme_minimal() +
  theme(legend.title = element_blank()) + 
  geom_smooth(method = "loess", color = "black", size = 0.5) + 
  theme(strip.text = element_text(face = "bold", size = 10))
our.plot

# Plot the intersection between the "80 percent power" line and the loess line.
smooths = ggplot_build(our.plot)
smooths = smooths$data[[2]]
# Approximate the intercept, representing the ATE that can be assessed at 80 percent power. 
smooth_1_i = c()
for (i in 1:7) {
  smooth_1_i[i] = smooths$x[which.min(abs(smooths$y[smooths$PANEL==i] - 0.8))]
}
round(smooth_1_i, 2)

# Annotations. 
dat_text = data.frame(
  label = c("d = 0.80 for \ndetecting ATE = 0.30",
            "d = 0.80 for \ndetecting ATE = 0.43",
            "d = 0.80 for \ndetecting ATE = 0.85",
            "d = 0.80 for \ndetecting ATE = 0.44",
            "d = 0.80 for \ndetecting ATE = 0.59",
            "d = 0.80 for \ndetecting ATE = 0.47",
            "d = 0.80 for \ndetecting ATE = 0.47"),
  our.data   = c("Full Sample", "Democrats", "Independents (or Unsure)", "Republicans", 
                 "Liberals", "Moderates (or Unsure)", "Conservatives")
)

# Add annotations to plot based on power analysis above. 
our.plot = our.plot + geom_text(
  data    = dat_text,
  mapping = aes(x = 1, y = 0, label = label),
  hjust   = "inward",
  vjust   = "inward",
  size = 3
)
our.plot = our.plot + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) 
our.plot
ggsave("2012-Gays-Power-Analysis.pdf", plot = our.plot, height = 5, width = 8.5, units = c("in"))
#----------
# MUSLIM list experiment, 2012. 
# Subset the data to include only power estimates for the relevant experiment. 
plot.muslims.2012 = subset(plot.data, our.data=='muslims.experiment.2012' | 
                             our.data=='muslims.experiment.cons.2012' | 
                             our.data=='muslims.experiment.libs.2012' | 
                             our.data=='muslims.experiment.mods.2012' |
                             our.data=='muslims.experiment.gop.2012' |
                             our.data=='muslims.experiment.dems.2012' |
                             our.data=='muslims.experiment.inds.2012')
# Rename the indicator variable denoting the samples or subsamples pertaining
# to the power estimates. 
plot.muslims.2012$our.data = car::recode(plot.muslims.2012$our.data, 
                                         "'muslims.experiment.2012' = 'Full Sample';
                                        'muslims.experiment.cons.2012' = 'Conservatives';
                                        'muslims.experiment.libs.2012' = 'Liberals';
                                        'muslims.experiment.mods.2012' = 'Moderates (or Unsure)';
                                        'muslims.experiment.gop.2012' = 'Republicans';
                                        'muslims.experiment.dems.2012' = 'Democrats';
                                        'muslims.experiment.inds.2012' = 'Independents (or Unsure)'")
# Reorder the levels in this indicator variable. 
plot.muslims.2012$our.data = factor(plot.muslims.2012$our.data, levels = c("Full Sample",
                                                                           "Democrats",
                                                                           "Independents (or Unsure)",
                                                                           "Republicans",
                                                                           "Liberals",
                                                                           "Moderates (or Unsure)",
                                                                           "Conservatives"))
table(plot.muslims.2012$our.data)

# Plot power as a function of effect size, and facet the plot by sample or subsample we're analyzing.
our.plot = ggplot(data = plot.muslims.2012, aes(effect_size, power, group = our.data)) +
  # geom_line() +
  geom_hline(yintercept = 0.80, linetype = "dashed") + 
  ggtitle("'Muslim Candidate' List Experiment, CCAP 2012: \nPower for Difference-of-Means Estimator, by ATE and Experiment Subset") +
  xlab("Average Treatment Effect") + 
  ylab("Power (Dashed Line is d = 0.80)") +
  facet_wrap(~ our.data, nrow = 2) + 
  theme_minimal() +
  theme(legend.title = element_blank()) + 
  geom_smooth(method = "loess", color = "black", size = 0.5) + 
  theme(strip.text = element_text(face = "bold", size = 10))
our.plot

# Plot the intersection between the "80 percent power" line and the loess line.
smooths = ggplot_build(our.plot)
smooths = smooths$data[[2]]
# Approximate the intercept, representing the ATE that can be assessed at 80 percent power. 
smooth_1_i = c()
for (i in 1:7) {
  smooth_1_i[i] = smooths$x[which.min(abs(smooths$y[smooths$PANEL==i] - 0.8))]
}
round(smooth_1_i, 2)

# Annotations. 
dat_text = data.frame(
  label = c("d = 0.80 for \ndetecting ATE = 0.29",
            "d = 0.80 for \ndetecting ATE = 0.42",
            "d = 0.80 for \ndetecting ATE = 0.77",
            "d = 0.80 for \ndetecting ATE = 0.47",
            "d = 0.80 for \ndetecting ATE = 0.53",
            "d = 0.80 for \ndetecting ATE = 0.49",
            "d = 0.80 for \ndetecting ATE = 0.47"),
  our.data   = c("Full Sample", "Democrats", "Independents (or Unsure)", "Republicans", 
                 "Liberals", "Moderates (or Unsure)", "Conservatives")
)

# Add annotations to plot based on power analysis above. 
our.plot = our.plot + geom_text(
  data    = dat_text,
  mapping = aes(x = 1, y = 0, label = label),
  hjust   = "inward",
  vjust   = "inward",
  size = 3
)
our.plot = our.plot + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) 
our.plot
ggsave("2012-Muslims-Power-Analysis.pdf", plot = our.plot, height = 5, width = 8.5, units = c("in"))
#----------
# MORMON list experiment, 2012. 
# Subset the data to include only power estimates for the relevant experiment. 
plot.mormons.2012 = subset(plot.data, our.data=='mormons.experiment.2012' | 
                             our.data=='mormons.experiment.cons.2012' | 
                             our.data=='mormons.experiment.libs.2012' | 
                             our.data=='mormons.experiment.mods.2012' |
                             our.data=='mormons.experiment.gop.2012' |
                             our.data=='mormons.experiment.dems.2012' |
                             our.data=='mormons.experiment.inds.2012')
# Rename the indicator variable denoting the samples or subsamples pertaining
# to the power estimates. 
plot.mormons.2012$our.data = car::recode(plot.mormons.2012$our.data, 
                                         "'mormons.experiment.2012' = 'Full Sample';
                                        'mormons.experiment.cons.2012' = 'Conservatives';
                                        'mormons.experiment.libs.2012' = 'Liberals';
                                        'mormons.experiment.mods.2012' = 'Moderates (or Unsure)';
                                        'mormons.experiment.gop.2012' = 'Republicans';
                                        'mormons.experiment.dems.2012' = 'Democrats';
                                        'mormons.experiment.inds.2012' = 'Independents (or Unsure)'")
# Reorder the levels in this indicator variable. 
plot.mormons.2012$our.data = factor(plot.mormons.2012$our.data, levels = c("Full Sample",
                                                                           "Democrats",
                                                                           "Independents (or Unsure)",
                                                                           "Republicans",
                                                                           "Liberals",
                                                                           "Moderates (or Unsure)",
                                                                           "Conservatives"))
table(plot.mormons.2012$our.data)

# Plot power as a function of effect size, and facet the plot by sample or subsample we're analyzing.
our.plot = ggplot(data = plot.mormons.2012, aes(effect_size, power, group = our.data)) +
  # geom_line() +
  geom_hline(yintercept = 0.80, linetype = "dashed") + 
  ggtitle("'Mormon Candidate' List Experiment, CCAP 2012: \nPower for Difference-of-Means Estimator, by ATE and Experiment Subset") +
  xlab("Average Treatment Effect") + 
  ylab("Power (Dashed Line is d = 0.80)") +
  facet_wrap(~ our.data, nrow = 2) + 
  theme_minimal() +
  theme(legend.title = element_blank()) + 
  geom_smooth(method = "loess", color = "black", size = 0.5) + 
  theme(strip.text = element_text(face = "bold", size = 10))
our.plot

# Plot the intersection between the "80 percent power" line and the loess line.
smooths = ggplot_build(our.plot)
smooths = smooths$data[[2]]
# Approximate the intercept, representing the ATE that can be assessed at 80 percent power. 
smooth_1_i = c()
for (i in 1:7) {
  smooth_1_i[i] = smooths$x[which.min(abs(smooths$y[smooths$PANEL==i] - 0.8))]
}
round(smooth_1_i, 2)

# Annotations. 
dat_text = data.frame(
  label = c("d = 0.80 for \ndetecting ATE = 0.30",
            "d = 0.80 for \ndetecting ATE = 0.43",
            "d = 0.80 for \ndetecting ATE = 0.78",
            "d = 0.80 for \ndetecting ATE = 0.47",
            "d = 0.80 for \ndetecting ATE = 0.58",
            "d = 0.80 for \ndetecting ATE = 0.51",
            "d = 0.80 for \ndetecting ATE = 0.47"),
  our.data   = c("Full Sample", "Democrats", "Independents (or Unsure)", "Republicans", 
                 "Liberals", "Moderates (or Unsure)", "Conservatives")
)

# Add annotations to plot based on power analysis above. 
our.plot = our.plot + geom_text(
  data    = dat_text,
  mapping = aes(x = 1, y = 0, label = label),
  hjust   = "inward",
  vjust   = "inward",
  size = 3
)
our.plot = our.plot + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) 
our.plot
ggsave("2012-Mormons-Power-Analysis.pdf", plot = our.plot, height = 5, width = 8.5, units = c("in"))
#----------
# POOLED SENSITIVE-ITEMS "prejudice index," 2012. 
# Subset the data to include only power estimates for the relevant experiment. 
plot.pooled.2012 = subset(plot.data, our.data=='pooled.experiment.2012' | 
                            our.data=='pooled.experiment.cons.2012' | 
                            our.data=='pooled.experiment.libs.2012' | 
                            our.data=='pooled.experiment.mods.2012' |
                            our.data=='pooled.experiment.gop.2012' |
                            our.data=='pooled.experiment.dems.2012' |
                            our.data=='pooled.experiment.inds.2012')
# Rename the indicator variable denoting the samples or subsamples pertaining
# to the power estimates. 
plot.pooled.2012$our.data = car::recode(plot.pooled.2012$our.data, 
                                        "'pooled.experiment.2012' = 'Full Sample';
                                        'pooled.experiment.cons.2012' = 'Conservatives';
                                        'pooled.experiment.libs.2012' = 'Liberals';
                                        'pooled.experiment.mods.2012' = 'Moderates (or Unsure)';
                                        'pooled.experiment.gop.2012' = 'Republicans';
                                        'pooled.experiment.dems.2012' = 'Democrats';
                                        'pooled.experiment.inds.2012' = 'Independents (or Unsure)'")
# Reorder the levels in this indicator variable. 
plot.pooled.2012$our.data = factor(plot.pooled.2012$our.data, levels = c("Full Sample",
                                                                         "Democrats",
                                                                         "Independents (or Unsure)",
                                                                         "Republicans",
                                                                         "Liberals",
                                                                         "Moderates (or Unsure)",
                                                                         "Conservatives"))
table(plot.pooled.2012$our.data)

# Plot power as a function of effect size, and facet the plot by sample or subsample we're analyzing.
our.plot = ggplot(data = plot.pooled.2012, aes(effect_size, power, group = our.data)) +
  # geom_line() +
  geom_hline(yintercept = 0.80, linetype = "dashed") + 
  ggtitle("Generalized Prejudice: Pooled Sensitive-Items from List Experiments, CCAP 2012: \nPower for Difference-of-Means Estimator, by ATE and Experiment Subset") +
  xlab("Average Treatment Effect") + 
  ylab("Power (Dashed Line is d = 0.80)") +
  facet_wrap(~ our.data, nrow = 2) + 
  theme_minimal() +
  theme(legend.title = element_blank()) + 
  geom_smooth(method = "loess", color = "black", size = 0.5) + 
  theme(strip.text = element_text(face = "bold", size = 10))
our.plot

# Plot the intersection between the "80 percent power" line and the loess line.
smooths = ggplot_build(our.plot)
smooths = smooths$data[[2]]
# Approximate the intercept, representing the ATE that can be assessed at 80 percent power. 
smooth_1_i = c()
for (i in 1:7) {
  smooth_1_i[i] = smooths$x[which.min(abs(smooths$y[smooths$PANEL==i] - 0.8))]
}
round(smooth_1_i, 2)

# Annotations. 
dat_text = data.frame(
  label = c("d = 0.80 for \ndetecting ATE = 0.22",
            "d = 0.80 for \ndetecting ATE = 0.34",
            "d = 0.80 for \ndetecting ATE = 0.64",
            "d = 0.80 for \ndetecting ATE = 0.35",
            "d = 0.80 for \ndetecting ATE = 0.47",
            "d = 0.80 for \ndetecting ATE = 0.39",
            "d = 0.80 for \ndetecting ATE = 0.38"),
  our.data   = c("Full Sample", "Democrats", "Independents (or Unsure)", "Republicans", 
                 "Liberals", "Moderates (or Unsure)", "Conservatives")
)

# Add annotations to plot based on power analysis above. 
our.plot = our.plot + geom_text(
  data    = dat_text,
  mapping = aes(x = 1, y = 0, label = label),
  hjust   = "inward",
  vjust   = "inward",
  size = 3
)
our.plot = our.plot + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) 
our.plot
ggsave("2012-Pooled-Power-Analysis.pdf", plot = our.plot, height = 5, width = 8.5, units = c("in"))
#----------

